import Overlay from "ol/Overlay";

/**
 * @typedef {Overlay | Overlay[]} overlayParams 需要操作的overlay参数
 */

/**
 * 获取overlay的groupId
 * @param {Overlay} overlay - 需要获取groupId的overlay对象
 * @returns {string|undefined} groupId
 */
const getGroupId = overlay => {
  return overlay.getGroupId
    ? overlay.getGroupId()
    : overlay.groupId || overlay.get("groupId");
};

/**
 * 将overlay参数转换为Overlay数组
 * @param {Overlay|Overlay[]} overlayParams - 需要操作的overlay参数
 * @returns {Overlay[]} 符合条件的overlay数组
 */
const transformOverlay = overlayParams => {
  return (
    Array.isArray(overlayParams) ? overlayParams : [overlayParams]
  ).filter(item => item instanceof Overlay);
};

/**
 * 根据id获取overlay
 * @param {import('ol/Map').default} map - 地图对象
 * @param {string} id - overlay的id
 * @returns {Overlay|undefined} 匹配的overlay对象
 */
export const getOverlayById = (map, id) => {
  return map.getOverlayById(id);
};

/**
 * 根据groupId获取overlay数组
 * @param {import('ol/Map').default} map - 地图对象
 * @param {string} groupId - overlay的groupId
 * @returns {Overlay[]} 匹配的overlay数组
 */
export const getOverlayByGroupId = (map, groupId) => {
  const overlays = [];
  map
    .getOverlays()
    .getArray()
    .forEach(overlay => {
      let groupId_ = getGroupId(overlay);

      if (groupId_ === groupId) {
        overlays.push(overlay);
      }
    });

  return overlays;
};

/**
 * 根据自定义条件获取overlay数组
 * @param {import('ol/Map').default} map - 地图对象
 * @param {function(Overlay, string, string, Overlay[]): boolean} condition - 条件函数，返回true则保留
 * @returns {Overlay[]} 匹配的overlay数组
 */
export const getOverlayByCondition = (map, condition) => {
  const result = [];

  const overlays = map.getOverlays().getArray();

  overlays.forEach(overlay => {
    if (condition(overlay, overlay.getId(), getGroupId(overlay), overlays)) {
      result.push(overlay);
    }
  });
  return result;
};

/**
 * 移除overlay
 * @param {import('ol/Map').default} map - 地图对象
 * @param {Overlay|Overlay[]} overlay - 需要移除的overlay或overlay数组
 * @returns {void}
 */
export const removeOverlay = (map, overlay) => {
  overlay = transformOverlay(overlay);

  overlay.forEach(item => {
    map.removeOverlay(item);
  });
};

/**
 * 清除所有overlay
 * @param {import('ol/Map').default} map - 地图对象
 * @returns {void}
 */
export const clearOverlay = map => {
  const overlays = map.getOverlays();
  overlays.clear();
};

/**
 * 设置overlay的可见性
 * @param {import('ol/Map').default} map - 地图对象
 * @param {Overlay|Overlay[]} overlay - 需要设置可见性的overlay或overlay数组
 * @param {boolean} visibility - 是否可见
 * @returns {void}
 */
export const setOverlayVisibility = (map, overlay, visibility) => {
  const overlays = transformOverlay(overlay);

  overlays.forEach(overlayItem => {
    if (overlayItem.setVisibility) {
      overlayItem.setVisibility(visibility);
    } else {
      const element = overlayItem.getElement();
      element.parentElement.style.display = visibility ? "flex" : "none";
    }
  });
};

/**
 * 切换overlay的可见性
 * @param {import('ol/Map').default} map - 地图对象
 * @param {Overlay|Overlay[]} overlay - 需要切换可见性的overlay或overlay数组
 * @returns {void}
 */
export const toggleOverlayVisibility = (map, overlay) => {
  const overlays = transformOverlay(overlay);

  overlays.forEach(overlayItem => {
    if (overlayItem.setVisibility && overlayItem.getVisibility) {
      overlayItem.setVisibility(!overlayItem.getVisibility());
    } else {
      const element = overlayItem.getElement();
      const visibility = element.parentElement.style.display !== "none";
      element.parentElement.style.display = visibility ? "none" : "flex";
    }
  });
};
